package org.sanelli.InterstellarGolf.SpecificObject;

import java.util.ArrayList;

import org.sanelli.InterstellarGolf.GenericObject.BoundingBox;
import org.sanelli.InterstellarGolf.GenericObject.GameEntity;
import org.sanelli.InterstellarGolf.GenericObject.Vector2D;

import org.sanelli.InterstellarGolf.R;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;

public class GameWorld {
	private GamePlayer _player;
	private ArrayList<GameNPC> _npc;
	private Context _context;
	private BoundingBox _boundingbox;
	private ArrayList<GameEntity> _entity;
	
	public GameWorld(Context context) {
		this._npc = new ArrayList<GameNPC>();
		this._entity = new ArrayList<GameEntity>();
		this._context = context;
	}
	
	public void LoadGame() {
		this._player = new GamePlayer(_context, new Vector2D(0, 363104*Math.pow(10, 3)));
		this._player.setImage(BitmapFactory.decodeResource(_context.getResources(), R.drawable.luna));
		this._entity.add(_player);
		
		GameNPC npc = new GameNPC(_context, new Vector2D(0,0));
		npc.setMass(5.9742*Math.pow(10, 24));
		npc.setImage(BitmapFactory.decodeResource(_context.getResources(), R.drawable.terra));
		this._npc.add(npc);
		this._entity.add(npc);
		
		npc = new GameNPC(_context, new Vector2D(0,0));
		npc.setMass(0);
		npc.set_BoundingBox(new BoundingBox(new Vector2D(-3.5E8, +5.5E8), new Vector2D(3.5E8, -5.5E8)));
		
		this._npc.add(npc);
		this._entity.add(npc);		
		
		initBoundingBox();
		
	}

	/*TODO non va*/
	public void initBoundingBox() {
		Vector2D p1 = new Vector2D();
		Vector2D p2 = new Vector2D();
		BoundingBox bb = null;
		for (GameEntity entity : this._entity ) {
			bb = entity.get_BoundingBox();
			if (bb.getP1().getX() < p1.getX())
				p1.setX(bb.getP1().getX());
			if (bb.getP1().getY() > p1.getY())
				p1.setY(bb.getP1().getY());
			if (bb.getP2().getX() > p2.getX())
				p2.setX(bb.getP2().getX());
			if (bb.getP2().getY() < p2.getY())
				p2.setY(bb.getP2().getY());
		}
		this._boundingbox = new BoundingBox(p1, p2);
	}
	
	public ArrayList<GameNPC> getGameNPC() {
		return this._npc;
	}

	public GamePlayer getPlayer() {
		return this._player;
	}
	
	public void updatePhysic(long timesincelastframe) {
		this._player.updatePhysic(_npc, timesincelastframe);
	}

	public BoundingBox getBoundingbox() {
		return _boundingbox;
	}

	public void setBoundingbox(BoundingBox _boundingbox) {
		this._boundingbox = _boundingbox;
	}
	
	public void onDraw(Canvas canvas) {
		for (GameNPC npc : this._npc ) {
			npc.onDraw(canvas);
		}
		this._player.onDraw(canvas);
	}
}
